package com.sskj.one.mine;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.hjq.toast.ToastUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.permissions.RxPermissions;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.Constans;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.CopyUtil;
import com.sskj.one.R;
import com.sskj.one.bean.CompanyBankBean;
import com.sskj.one.util.ArithUtil;
import com.sskj.one.view.SelectPictureDialog;
import com.zzhoujay.richtext.RichText;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/06/15
 */
public class DownGouhuiActivity extends BaseActivity<DownGouhuiPresenter> {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_money_cny)
    TextView tv_money_cny;
    @BindView(R.id.rb_alipay)
    RadioButton rbAlipay;
    @BindView(R.id.rb_bank)
    RadioButton rbBank;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_account_alipay)
    EditText etAccountAlipay;
    @BindView(R.id.ll_alipay)
    LinearLayout llAlipay;
    @BindView(R.id.et_bank_name)
    EditText etBankName;
    //    @BindView(R.id.et_branch)
//    EditText etBranch;
//    @BindView(R.id.et_bank_card)
//    EditText etBankCard;
    @BindView(R.id.ll_bank)
    LinearLayout llBank;
    @BindView(R.id.tv_qrcode)
    TextView tvQrcode;
    @BindView(R.id.iv_qrcode)
    ImageView ivQrcode;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @BindView(R.id.tv_shoukuan_name)
    TextView tvShoukuanName;
    @BindView(R.id.tv_shoukuan_bank)
    TextView tvShoukuanBank;
    @BindView(R.id.tv_shoukuan_bankcard)
    TextView tvShoukuanBankcard;
    @BindView(R.id.iv_copy_bank)
    ImageView ivCopyBank;
    @BindView(R.id.tv_shoukuan_branch)
    TextView tvShoukuanBranch;
    @BindView(R.id.et_bank)
    EditText etBank;
    @BindView(R.id.tv_notice)
    TextView tv_notice;

    private String tongdao;
    private String gouHuiMOney;
    private String nowRate;//点击线下购汇时当前的汇率
    private SelectPictureDialog selectPictureDialog;
    private String mImagePath;

    private String type = Constans.ALIPAY;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_down_gouhui;
    }

    @Override
    public DownGouhuiPresenter getPresenter() {
        return new DownGouhuiPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("购汇");
        tongdao = getIntent().getStringExtra("tongdao");
        gouHuiMOney = getIntent().getStringExtra("gouHuiMOney");
        nowRate = getIntent().getStringExtra("nowRate");
        tvMoney.setText("$ " + gouHuiMOney);
        tv_money_cny.setText("≈" + NumberUtil.keep(String.valueOf(ArithUtil.mul(Double.valueOf(gouHuiMOney), Double.valueOf(nowRate))), 5) + "元");
        ivQrcode.setImageResource(R.mipmap.icon_upload);
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.rb_alipay:
                    type = Constans.ALIPAY;
                    llAlipay.setVisibility(View.VISIBLE);
                    llBank.setVisibility(View.GONE);
                    break;
                case R.id.rb_bank:
                    type = Constans.BANK;
                    llAlipay.setVisibility(View.GONE);
                    llBank.setVisibility(View.VISIBLE);
                    break;
            }
        });
    }

    @Override
    public void initData() {
        mPresenter.getShoukuanMsg();
        ClickUtil.click(300, ivCopyBank, () -> {
            if (isEmpty(tvShoukuanBankcard)) {
                ToastUtils.show("获取收款信息失败");
                return;
            }
            CopyUtil.copy(getText(tvShoukuanBankcard));
        });
        ClickUtil.click(300, btnConfirm, () -> {
            if (type.equals(Constans.ALIPAY)) {
                if (isEmpty(etName)) {
                    toastHint(etName);
                    return;
                }
                if (isEmpty(etAccountAlipay)) {
                    toastHint(etAccountAlipay);
                    return;
                }
                if (isEmpty(mImagePath)) {
                    ToastUtils.show("请先上传凭证");
                    return;
                }
                mPresenter.gouhui(gouHuiMOney, tongdao, type, getText(etName), getText(etAccountAlipay), "", "", "", new File(mImagePath));
            } else if (type.equals(Constans.BANK)) {
                if (isEmpty(etBankName)) {
                    toastHint(etBankName);
                    return;
                }
                if (isEmpty(etBank)) {
                    toastHint(etBank);
                    return;
                }
//                if (isEmpty(etBranch)) {
//                    toastHint(etBranch);
//                    return;
//                }
//                if (isEmpty(etBankCard)) {
//                    toastHint(etBankCard);
//                    return;
//                }
//                if (getText(etBankCard).length() < 14) {
//                    ToastUtils.show("请输入正确的银行卡号");
//                    return;
//                }
                if (isEmpty(mImagePath)) {
                    ToastUtils.show("请先上传凭证");
                    return;
                }
                mPresenter.gouhui(gouHuiMOney, tongdao, type, getText(etBankName), "", getText(etBank), "", "", new File(mImagePath));
//                mPresenter.gouhui(gouHuiMOney, tongdao, type, getText(etBankName), "", getText(etBank), getText(etBranch), getText(etBankCard), new File(mImagePath));
            }
        });
        ClickUtil.click(300, ivQrcode, () -> {
            new RxPermissions(DownGouhuiActivity.this)
                    .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                    .subscribe(granted -> {
                        if (granted) {
                            selectPic();
                        }
                    });
        });
    }

    private void selectPic() {
        if (selectPictureDialog == null) {
            selectPictureDialog = new SelectPictureDialog(this);
        }
        selectPictureDialog.setOnSelectListener(new SelectPictureDialog.SelectListener() {
            @Override
            public void selectByAlbum() {
                PictureSelector.create(DownGouhuiActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }

            @Override
            public void selectByPhoto() {
                PictureSelector.create(DownGouhuiActivity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .setOutputCameraPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }
        });
        selectPictureDialog.show();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片、视频、音频选择结果回调
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            mImagePath = TextUtils.isEmpty(selectList.get(0).getCompressPath()) ? selectList.get(0).getPath() : selectList.get(0).getCompressPath();
            ImageUtil.setImage(mImagePath, ivQrcode);
        }
    }


    public static void start(Context context, String tongdao, String gouHuiMOney, String nowRate) {
        Intent intent = new Intent(context, DownGouhuiActivity.class);
        intent.putExtra("tongdao", tongdao);
        intent.putExtra("gouHuiMOney", gouHuiMOney);
        intent.putExtra("nowRate", nowRate);
        context.startActivity(intent);
    }

    public void getShoukuanMsgOK(CompanyBankBean data) {
        if (data == null) {
            return;
        }
        tvShoukuanName.setText("收款人：" + data.getBank().getName());
        tvShoukuanBank.setText(data.getBank().getBank() + "：");
        tvShoukuanBranch.setText("开户支行：" + data.getBank().getBranch());
        tvShoukuanBankcard.setText(data.getBank().getBank_car());
        RichText.fromHtml(data.getNotice().getContent()).into(tv_notice);
    }

    public void gouhuiOK() {
        ToastUtils.show("购汇成功,待审核");
        finish();
    }

    private String formatImg(String law_agree) {
        Document document = Jsoup.parse(law_agree);
        Elements imgs = document.getElementsByTag("img");
        for (Element img : imgs) {
            img.attr("width", "100%").attr("height", "auto");
            String url = img.attr("src");
            if (!url.contains("http")) {
                url = BaseHttpConfig.BASE_URL + url;
            }
            img.attr("src", url);
        }
        return document.toString();
    }
}
